/*
 * 点击关键字跳转到指定公司,此方法主要来解决GET提交乱码问题
 * @param {Object} name
 */
function findCompa(name, tar) {
	name = name || "";
	tar = tar || "_blank";
	name = encodeURIComponent(encodeURIComponent(name));
	window.open("company.html?method=findCompany&name=" + name, tar, "");
}

// RFC #822 DATE AND TIME SPECIFICATION
var month = {
	 "Jan": "1",
	 "Feb": "2",
	 "Mar": "3",
	 "Apr": "4",
     "May": "5",
     "Jun": "6",
     "Jul": "7",
     "Aug": "8",
     "Sep": "9",
     "Oct": "10",
     "Nov": "11",
     "Dec": "12"
};

function createNewItem(res) {
	var r = res.responseData.results;
	var dis = "<ul class='newsUL'><li style='height:5px;'></li>";
	for (var i = 0; i < r.length; i++) {
		var publishDate = r[i].publishedDate; // Wed, 27 Jul 2011 19:24:01 -0700
		publishDate = parseDate(publishDate);
		dis += "<li class='newsLI'><a href="+ r[i].unescapedUrl +" style='float:left;'>"+ r[i].title +"</a>("+ r[i].publisher +")<br/>  <p style='float:right;'>"+ publishDate +"</p>";
		if (i < r.length -1) {
			dis += "<p style='padding:0px;margin:4px;border:0px;border-top:1px dotted #DDD;clear:both;'></p>";
		}
		dis += "</li>";
	}
	dis += "</ul>";
	return dis;
}
function createNewItemFor(res) {
	var r = res.responseData.results;
	var dis = "<ul class='newsUL' style='margin:0px;padding:0px;'><li style='height:5px;'></li>";
	for (var i = 0; i < r.length; i++) {
		var publishDate = r[i].publishedDate; // Wed, 27 Jul 2011 19:24:01 -0700
		publishDate = parseDate(publishDate);
		dis += "<li class='newsLI' style='line-height:20px;'><a href="+ r[i].unescapedUrl +" style='float:left;font-weight:bold;'>"+ r[i].title +"</a> ("+ r[i].publisher +")   "+ publishDate +"";
		dis += "<p>" + r[i].content + "</p>";
		// 显示图片
		if (r[i].image) {
			var imgs = r[i].image;
			dis += "<img src='"+ imgs.url  +"'>";
		}
		// 显示相关新闻
		if (r[i].relatedStories) {
			var rss = r[i].relatedStories;
			dis += "<p>";
			dis += "<label style='color:red;'>相关新闻：</label>";
			for (var j = 0; j < rss.length; j++) {
				var rs = rss[j];
				dis += "<a href='"+ rs.unescapedUrl +"' target='_blank'>"+ rs.title +"</a> &nbsp;&nbsp; ";
			}
			dis += "</p>";
		}
		if (i < r.length -1) {
			dis += "<p style='padding:0px;margin:4px;border:0px;border-top:1px dotted #DDD;clear:both;'></p>";
		}
		dis += "</li>";
	}
	dis += "</ul>";
	return dis;
}
function parseDate(date) {
	var ts = date.split(" ");
	var res = month[ts[2]] + "月" + ts[1] + "日 " + ts[4];
	return res;
}

// -----------------------
// QQ表情相关方法
// ----------------------
function showFace(objid, dispid) {
	if (window.dis) {
		var div = document.getElementById("_face_div");
		document.getElementById(objid).parentNode.removeChild(div);
		window.dis = false;
		return;
	}
	window.dis = true;
	var div = document.createElement("div");
	div.id = "_face_div";
	div.style.position = "absolute";
	div.style["z-index"] = "1000";
	div.style.border = "0px solid blue";
	div.style.width = "455px";
	div.style.height = "220px";
	div.style.margin = "0px auto auto 8px";
	div.style.padding = "0px";
	div.style.display = "inline"; // important
	div.style.backgroundColor = "transparent";
	
	var iframe = document.createElement("iframe");
	iframe.width = "100%";
	iframe.height = "100%";
	iframe.style.backgroundColor = "transparent";
	iframe.style.display = "inline";
	iframe.style.position = "absolute";
	iframe.style["z-index"] = "2000";
	iframe.style.border = "0px solid red";
	div.appendChild(iframe);
	
	var imgdiv = document.createElement("div");
	imgdiv.style.position = "absolute";
	imgdiv.style["z-index"] = "3000";
	imgdiv.style.border = "1px solid #CCC";
	imgdiv.style.width = "455px";
	imgdiv.style.height = "220px";
	imgdiv.style.margin = "0px";
	imgdiv.style.margin = "0px";
	imgdiv.style.display = "inline"; // important
	imgdiv.style.backgroundColor = "#FFF";
	div.appendChild(imgdiv);

	var imgs = "";
	for(var k=0; k<105; k++){
		imgs += "<img  onmousemove=\"this.style.borderColor='blue'\" onmouseout=\"this.style.borderColor='#FFF'\" style='margin:2px;border:1px solid #FFF;' title="+ k +" src='images/faces/"+ k +".gif' onclick=\"select('"+objid+"','"+dispid+"',this.title);\">"
	}
	
	imgdiv.innerHTML = imgs;
	var obj = document.getElementById(objid);
	obj.parentNode.insertBefore(div, obj.nextSibling);
}
function select(objid, dispid, title) {
	var div = document.getElementById("_face_div");
	document.getElementById(objid).parentNode.removeChild(div);
	window.dis = false;
	insertText(document.getElementById(dispid),"[" + title + "]");
}
//在光标位置插入文字
function insertText(obj,str) {
	obj.focus();
	if (document.selection) {
		var sel = document.selection.createRange();
		sel.text = str;
	}
	else if (typeof obj.selectionStart == 'number' && typeof obj.selectionEnd == 'number') {
		var startPos = obj.selectionStart,
			endPos = obj.selectionEnd,
			cursorPos = startPos,
			tmpStr = obj.value;
		obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);
		cursorPos += str.length;obj.selectionStart = obj.selectionEnd = cursorPos;
	}
	else {
		obj.value += str;
	}
}
